package com.sgb.kjwl.view.widget;

import android.content.Context;
import androidx.recyclerview.widget.GridLayoutManager;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.DeviceFilterEntity;
import com.sgb.jicai.entity.FilterEntity;
import com.sgb.kjwl.model.entity.MachineFilterEntity;
import com.sgb.kjwl.view.ui.adapter.device.FilterAdapter;
import com.sgb.kjwl.view.ui.adapter.device.ParentFilterAdapter;
import com.swgk.core.customview.MaxRecyclerView;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.util.MLog;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class FliterPopWindow extends BasePopWindow {
    private String tag;
    private LinearLayout rootLayout;
    private MaxRecyclerView recyclerView;
    private MaxRecyclerView childView;
    private TextView okView;
    private TextView resetView;
    private FilterResultClick filterResultClick;
    private List<FilterEntity<List<FilterEntity>>> filterEntityList;
    private List<MachineFilterEntity> arrtList;
    private ParentFilterAdapter parentFilterAdapter;
    private FilterAdapter filterAdapter;
    private List<String> noList;
    private List<DeviceFilterEntity> deviceFilterEntityList;
    private Map map;
    //第一级分类编号
    private String pNo = "";
    //第二级类型编号
    private String cNo = "";
    //第三级编号
    private String dNo = "";
    //第四级编号
    private String eNo = "";
    private boolean isReset = false;

    public interface FilterResultClick {
        void result(Object key, Object key2);

        void dimiss();

        void queryChild(String pNo, String cNo);
    }

    public void setFilterResultClick(FilterResultClick filterResultClick) {
        this.filterResultClick = filterResultClick;
    }

    public FliterPopWindow(Context context) {
        super(context, DIR_RIGHT);
    }

    @Override
    protected int popLayout() {
        return R.layout.popwindow_fliter;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        deviceFilterEntityList = new ArrayList<>();
        rootLayout = holder.getPop().findViewById(R.id.root_layout);
        recyclerView = holder.getPop().findViewById(R.id.filter_list);
        childView = holder.getPop().findViewById(R.id.child_list);
        okView = holder.getPop().findViewById(R.id.ok_view);
        resetView = holder.getPop().findViewById(R.id.reset_view);
        okView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dissMiss();
                if (tag.equals("DeviceOutFragment")) {
                    if (filterResultClick != null) {
                        noList = new ArrayList<>();
                        noList.clear();
                        noList.add(dNo);
                        if (!"".equals(eNo))
                            noList.add(eNo);
                        if (isReset)
                            noList.clear();
                        filterResultClick.result(noList, deviceFilterEntityList);
                    }
                }

            }
        });
        resetView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //第一级分类编号
                pNo = "";
                //第二级类型编号
                cNo = "";
                //第三级编号
                dNo = "";
                //第四级编号
                eNo = "";
                filterResultClick.queryChild(pNo, cNo);
                deviceFilterEntityList.clear();
                isReset = true;
                rootLayout.removeAllViews();
                parentFilterAdapter.setDefItem(-1);
                filterAdapter.setDefItem(-1);
            }
        });
        parentFilterAdapter = new ParentFilterAdapter(mContext, null);
        parentFilterAdapter.setItemSelect(true);
        parentFilterAdapter.setDefItem(-1);
        recyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        recyclerView.setAdapter(parentFilterAdapter);
        parentFilterAdapter.setOnItemClick(new AdapterOnItemClick<FilterEntity<List<FilterEntity>>>() {
            @Override
            public void onItemClick(FilterEntity<List<FilterEntity>> listFilterEntity, int position) {
                if (listFilterEntity.getChildren() == null || listFilterEntity.getChildren().size() == 0) {
                    rootLayout.removeAllViews();
                    deviceFilterEntityList.clear();
                    filterAdapter.setDatas(null);
                    return;
                }
                pNo = listFilterEntity.getNo();
                filterAdapter.setDefItem(0);
                filterAdapter.setDatas(listFilterEntity.getChildren());
                MLog.d("test", listFilterEntity.getName() + " " + listFilterEntity.getNo());
                if (filterResultClick != null) {
                    filterResultClick.queryChild(pNo, listFilterEntity.getChildren().get(0).getNo());
                }
            }
        });

        filterAdapter = new FilterAdapter(mContext, null, R.layout.item_filter_parent);
        filterAdapter.setItemSelect(true);
        filterAdapter.setDefItem(-1);
        childView.setLayoutManager(new GridLayoutManager(mContext, 3));
        childView.setAdapter(filterAdapter);
        filterAdapter.setOnItemClick(new AdapterOnItemClick<FilterEntity>() {
            @Override
            public void onItemClick(FilterEntity filterEntity, int position) {
                MLog.d("test", filterEntity.getName() + " " + filterEntity.getNo());
                cNo = filterEntity.getNo();
                if (filterResultClick != null) {
                    filterResultClick.queryChild(pNo, cNo);
                }
            }
        });
    }

    public void setData(List<FilterEntity<List<FilterEntity>>> datas, String tag) {
        this.filterEntityList = datas;
        this.tag = tag;
        parentFilterAdapter.setDatas(datas);

    }

    public void setChildView(List<FilterEntity<List<FilterEntity>>> datas) {
        rootLayout.removeAllViews();
        deviceFilterEntityList.clear();
        FilterEntity<List<FilterEntity>> entity;
        for (int i = 0; i < datas.size(); i++) {
            entity = datas.get(i);
            if (entity.getChildren() != null && entity.getChildren().size() > 0) {
                if (entity.getUnit() != null) {
                    addTitleView(entity.getName() + "(" + entity.getUnit() + ")");
                } else {
                    addTitleView(entity.getName());
                }
            }
            MLog.d("test", "标题 " + entity.getName() + "长度" + datas.size());
            addRecyclerView(entity.getChildren(), i, entity.getType());
        }
    }

    private void addTitleView(String title) {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.gravity = Gravity.CENTER_VERTICAL;
        TextView textView = new TextView(mContext);
        textView.setText(title);
        textView.setTextSize(16);
        textView.setTextColor(mContext.getResources().getColor(R.color.color_B6B6B6));
        rootLayout.addView(textView, layoutParams);
    }

    private void addRecyclerView(List<FilterEntity> children, final int i, final int type) {
        if (children == null || children.size() == 0) {
            return;
        }
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        FilterAdapter adapter = new FilterAdapter(mContext, null, R.layout.item_filter_parent);
        DeviceFilterEntity deviceFilterEntity = new DeviceFilterEntity();
        deviceFilterEntity.setNo(children.get(0).getNo());
        deviceFilterEntityList.add(deviceFilterEntity);
        adapter.setItemSelect(true);
        adapter.setDefItem(-1);
        MaxRecyclerView recyclerView = new MaxRecyclerView(mContext);
        recyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        recyclerView.setAdapter(adapter);
        adapter.setDatas(children);
        adapter.setOnItemClick(new AdapterOnItemClick<FilterEntity>() {
            @Override
            public void onItemClick(FilterEntity filterEntity, int position) {
                MLog.d("test", "child " + filterEntity.getName());
                if (tag.equals("DeviceOutFragment")) {
                    if (i == 0) {
                        dNo = filterEntity.getNo();
                    } else if (i == 1) {
                        eNo = filterEntity.getNo();
                    }
                    deviceFilterEntityList.get(i).setVal(filterEntity.getName());
                }
            }
        });
        rootLayout.addView(recyclerView, layoutParams);
    }


    @Override
    public void dismiss() {
        super.dismiss();
        if (filterResultClick != null) {
            filterResultClick.dimiss();
        }
    }
}
